package org.example.elm_springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.elm_springboot.service.UserService;
import org.example.elm_springboot.po.User;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:5174", allowCredentials = "true")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping("/getUserByIdByPass")
    public User getUserByIdByPass(String userId, String password) {
        return userService.getUserByIdByPass(userId, password);
    }
    
    @GetMapping("/getUserById")
    public int getUserById(String userId) {
        return userService.getUserById(userId);
    }
    
    @PostMapping("/saveUser")
    public int saveUser(String userId, String password, String userName, Integer userSex) {
        try {
            User user = new User();
            user.setUserId(userId);
            user.setPassword(password);
            user.setUserName(userName);
            user.setUserSex(userSex);
            return userService.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
} 